<?php
/**
 * Permission Service
 * User: zng
 * Date: 2019/11/4
 * Time: 14:34
 */
namespace app\admin\service\order;

use app\admin\model\order\Order as OrderModels;
use app\exceptions\OrdinaryException;
use think\Db;

Class Order {

    /**
     * 首页列表
     * @param array $get 筛选条件
     * @author zx
     * @date 2020-03-18
     * @return \think\Paginator
     */
    public function indexPaginate( array $get=[] )
    {
        $map = [];
        if ( get($get, 'food_title') ) {
            $map[] = ['food_title', 'LIKE', $get['food_title'].'%'];
        }
        if ( get($get, 'order_number') ) {
            $map[] = ['order_number', '=', $get['order_number']];
        }
        if ( get($get, 'handle') === 0 || get($get, 'handle') === '0' || get($get, 'handle') == 1 ) {
            $map[] = ['handle', '=', $get['handle']];
        }
        if ( get($get, 'username') ) {
            $map[] = ['username', '=', $get['username']];
        }
        if ( get($get, 'mobile') ) {
            $map[] = ['mobile', '=', $get['mobile']];
        }
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;
        return OrderModels::where($map)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 菜品分类编辑
     * @param OrderModels $model
     * @param array $data
     */
    public function save(OrderModels $model, array $data)
    {
        $model->username = $data['username'];
        $model->mobile = $data['mobile'];
        $model->address = $data['address'];
        $model->business_remarks = $data['business_remarks'];
        return $model->save();
    }

    /**
     * 新订单列表
     * @return \think\Paginator
     */
    public function handle($get)
    {
        $pageSize = get($get,'pageSize')>0?$get['pageSize']:20;
        return OrderModels::where('handle', 0)
            ->order('id', 'desc')
            ->paginate($pageSize);
    }

    /**
     * 处理订单
     * @param int|array $data
     * @return bool
     */
    public function complete($data, int $handle=1)
    {
        Db::startTrans();
        if ( $handle == 2 ) {
            $map = [];
            $map[] = is_array($data)?['handle','in',$data]:['handle','=',0];
            $order = OrderModels::with(['orderFoods'=>function($query){
                $query->with(['foods']);
            }])->where($map)->selectOrFail();
            foreach ( $order as $_order ) {
                foreach ( $_order->orderFoods as $_orderFoods ) {
                    if ( $_orderFoods->foods->save(['stock'=>$_orderFoods->foods->stock+$_orderFoods->number]) === false ) {
                        Db::rollback();
                        throw new OrdinaryException('库存返还失败!请重试');
                    }
                }
            }
        }
        $list = [];
        if ( is_array($data) ) {
            foreach ( $data as $_id ) {
                $list[] = ['id'=>$_id,'handle'=>$handle,'handle_at'=>date("Y-m-d H:i:s")];
            }
        } else {
            $list[] = ['id'=>$data, 'handle'=>$handle,'handle_at'=>date("Y-m-d H:i:s")];
        }
        $result = (new OrderModels())->saveAll($list);
        if ( false===$result->isEmpty() ) {
            Db::commit();
            return true;
        }
        Db::rollback();
        return false;
    }

    /**
     * 统计未处理订单数
     * @return int
     */
    public function count()
    {
        return OrderModels::where('handle',0)->count();
    }

}